package com.zytool.easyrestful.core.convert;

import java.util.Iterator;
import java.util.ServiceLoader;

public class ConvertorHolder {

    private int isInit = 0;
    private NamingConvertor convertor = null;

    private ConvertorHolder() {
        this.isInit = 0;
    }

    public static ConvertorHolder instance() {
        if (Inner.inst.isInit == 0) {
            synchronized (Inner.inst.convertor) {
                if (Inner.inst.isInit == 0) {
                    init();
                }
            }
        }
        return Inner.inst;
    }

    private static void init() {
        ServiceLoader<NamingConvertor> convertors = ServiceLoader.load(NamingConvertor.class);
        Iterator iterator = convertors.iterator();
        if (iterator.hasNext()) {
            Inner.inst.convertor = (NamingConvertor) iterator.next();
        } else {
            Inner.inst.convertor = new DefaultNamingConvertor();
        }
    }

    public NamingConvertor getConvertor() {
        return convertor;
    }

    public static class Inner {
        public static ConvertorHolder inst = new ConvertorHolder();

        private Inner() {
        }
    }
}
